//
//  NewsDetailCell.m
//  OneCard
//
//  Created by sally on 16/2/18.
//  Copyright © 2016年 koalac. All rights reserved.
//

#import "DateUtil.h"
#import "NewsDetailCell.h"
#import "ChatExtDB.h"

@interface NewsDetailCell ()

@property (weak, nonatomic) IBOutlet UIButton *timeBtn;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIImageView *contentBG;
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headLeftConstraint;
@property (weak, nonatomic) IBOutlet UIView *messageContentView;

@end

@implementation NewsDetailCell

- (void)awakeFromNib {
  [self initControl];
}

- (void)initControl {
  self.headImage.layer.cornerRadius = self.headImage.height / 2;
  self.headImage.layer.masksToBounds = YES;
  self.headImage.userInteractionEnabled = YES;

  self.contentLabel.preferredMaxLayoutWidth = ScreenWidth - GChatContentLeftMargin - GChatContentLeftMargin - 30;
  self.contentLabel.numberOfLines = 0;
}

- (void)setCellData:(MessageModel *)cellData {
  _cellData = cellData;

  //设置头像
  [self.headImage sd_setImageWithURL:[NSURL URLWithString:self.cellData.model.avatar] placeholderImage:[UIImage imageNamed:@"ic_default"]];

  //设置时间
  NSString *time = [DateUtil getFormatPubTime:self.cellData.model.dateline];
  [self.timeBtn setTitle:time forState:UIControlStateNormal];

  //设置内容
  self.contentLabel.text = self.cellData.text;

  //设置左右对话控件
  NSString *imageStr;
  CGFloat headLeftWidth = GNormalMargin;
  if (self.cellData.sender == MessageFrom_Mine) {
    self.contentLabel.textColor = [UIColor whiteColor];
    imageStr = @"chat_mine_bg.png";
    headLeftWidth = ScreenWidth - GNormalMargin - GHeadImageHeight;
  } else if (self.cellData.sender == MessageFrom_Other) {
    self.contentLabel.textColor = GBlackColor;
    imageStr = @"chat_other_bg.png";
    headLeftWidth = GNormalMargin;
  }
  if (imageStr) {
    UIImage *image = [UIImage imageNamed:imageStr];
    UIEdgeInsets insets = UIEdgeInsetsMake(2, 2, 2, 5);
    image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    [self.contentBG setImage:image];
    //    [self.contentBG setImage:[image stretchableImageWithLeftCapWidth:image.size.width / 2 topCapHeight:image.size.height / 2]];
    self.headLeftConstraint.constant = headLeftWidth;

    [self updateMessageContentConstraint];
    [self.contentView updateConstraintsIfNeeded]; //立即触发约束更新，自动更新布局
  }
}

- (void)setCellMsgData:(EMMessage *)message {
  
  //来源 背景 时间 头像 内容
  _cellMsgData = message;
  
  //来源
  NSString *imageStr;
  CGFloat headLeftWidth = GNormalMargin;
  if ([self.cellMsgData.from isEqualToString:self.cellMsgData.conversationChatter]) {//发送者是对方
    imageStr = @"chat_other_bg.png";
    headLeftWidth = GNormalMargin;
    self.contentLabel.textColor = GBlackColor;
    
    //数据库设置头像
    ChatExtDB *extDb = [ChatExtDB queryExtByUserName:message.conversationChatter];
    NSString *otherAvatar = extDb.avatar;
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:otherAvatar] placeholderImage:[UIImage imageNamed:@"ic_default"]];
  }else{
    imageStr = @"chat_mine_bg.png";
    headLeftWidth = ScreenWidth - GNormalMargin - GHeadImageHeight;
    self.contentLabel.textColor = [UIColor whiteColor];

    //本地缓存设置头像
    UserModel *mineModel = [[CacheManager shareInstance] getLastLoginAccount];
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:mineModel.user_data.user_profile_pic] placeholderImage:[UIImage imageNamed:@"ic_default"]];
  }
  
  if (imageStr) {
    //背景
    UIImage *image = [UIImage imageNamed:imageStr];
    [self.contentBG setImage:[image stretchableImageWithLeftCapWidth:image.size.width / 2 topCapHeight:image.size.height *3 / 4]];
    self.headLeftConstraint.constant = headLeftWidth;
    
    //内容
    EMTextMessageBody *body = self.cellMsgData.messageBodies[0];
    EMChatText *chat = (EMChatText *)body.chatObject ;
    self.contentLabel.text = chat.text;
    
    [self updateMessageContentConstraint];
    [self.contentView updateConstraintsIfNeeded];//立即触发约束更新，自动更新布局
  }
  
  //时间
  NSString *time = [DateUtil getFormatTime:self.cellMsgData.timestamp];
  [self.timeBtn setTitle:time forState:UIControlStateNormal];
}

#pragma mark 消息体布局
- (void)updateMessageContentConstraint {
  CommonMethod *common = [CommonMethod sharedCommonMethod];
  NSLayoutRelation leftRelation = NSLayoutRelationEqual;
  NSLayoutRelation rightRelation = NSLayoutRelationEqual;

  if ([self.cellMsgData.from isEqualToString:self.cellMsgData.conversationChatter]) {//发送者是对方
    leftRelation = NSLayoutRelationEqual;
    rightRelation = NSLayoutRelationGreaterThanOrEqual;
  } else {
    leftRelation = NSLayoutRelationGreaterThanOrEqual;
    rightRelation = NSLayoutRelationEqual;
  }

  NSLayoutConstraint *right = [common findSecondConstraint:self.contentView secondItem:self.messageContentView attribute:NSLayoutAttributeTrailing];
  if (right) {
    //移除
    [self.contentView removeConstraint:right];
  }

  //右约束
  NSLayoutConstraint *addRight = [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeTrailing relatedBy:rightRelation toItem:self.messageContentView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:GChatContentLeftMargin];
  [self.contentView addConstraint:addRight];

  NSLayoutConstraint *left = [common findFirstConstraint:self.contentView firstItem:self.messageContentView attribute:NSLayoutAttributeLeading];
  if (left) {
    //移除
    [self.contentView removeConstraint:left];
  }

  [self layoutIfNeeded];

  //左约束
  NSLayoutConstraint *addLeft = [NSLayoutConstraint constraintWithItem:self.messageContentView attribute:NSLayoutAttributeLeading relatedBy:leftRelation toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:GChatContentLeftMargin];
  [self.contentView addConstraint:addLeft];

  [self layoutIfNeeded];
}

@end
